<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useAppStore } from './store'
import TabBar from './components/common/TabBar.vue'

// 获取当前路由
const route = useRoute()
const appStore = useAppStore()

// 是否显示TabBar
const showTabBar = computed(() => route.meta.showTabBar)

// 当前激活的标签页
const activeTab = computed(() => {
  const path = route.path
  if (path === '/') return 'home'
  return path.substring(1) // 去掉开头的斜杠
})

// 监听activeTab变化，更新store中的状态
watch(activeTab, (newVal) => {
  appStore.setActiveTab(newVal)
})

// 页面加载完成后的初始化
onMounted(() => {
  // 设置初始激活的标签页
  appStore.setActiveTab(activeTab.value)
})
</script>

<template>
  <div class="app-container">
    <!-- 路由视图 -->
    <router-view v-slot="{ Component }">
      <transition name="fade" mode="out-in">
        <keep-alive>
          <component :is="Component" />
        </keep-alive>
      </transition>
    </router-view>
    
    <!-- 底部导航栏 -->
    <TabBar v-if="showTabBar" :active="activeTab" />
  </div>
</template>

<style>
/* 全局样式 */
body {
  margin: 0;
  padding: 0;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
}

.app-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
